package com.detrum.usbdevice.customview;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.detrum.base.BaseStatic;
import com.detrum.usbdevice.R;
import com.detrum.wheel.ArrayWheelAdapter;
import com.detrum.wheel.OnWheelChangedListener;
import com.detrum.wheel.WheelView;


/**
 * Created by 嗨哟 on 2017/1/11.
 */
public class ChannelSelectPopup implements View.OnClickListener, OnWheelChangedListener {
    private WheelView dialog_first, dialog_second;
    private TextView dialog_quxiao, dialog_queding, custom_channel, search_channel, current_channel;
    private LinearLayout select_channel;
    private LinearLayout select_ok_or_cancel;
    private Context context;
    private String[] strs;
    private String first, second;
    private static PopupWindow popupWindow;
    private ChannelPopupCall call;
    private int shi,ge;

    public ChannelSelectPopup(ChannelPopupCall call) {
        this.call = call;
    }

    public void setStrs(String[] strs) {
        this.strs = strs;
    }

    public void setChannel(int currentchannel){
        ge = currentchannel%10;
        shi = currentchannel/10;
    }

    public void init(Context context) {
        View view = View.inflate(context, R.layout.channel_select_popup, null);
        this.context = context;
        custom_channel = view.findViewById(R.id.custom_channel);
        search_channel = view.findViewById(R.id.search_channel);
        current_channel = view.findViewById(R.id.current_channel);
        select_channel = view.findViewById(R.id.select_channel);
        select_ok_or_cancel = view.findViewById(R.id.select_ok_or_cancel);

        dialog_quxiao = view.findViewById(R.id.dialog_quxiao);
        dialog_queding = view.findViewById(R.id.dialog_queding);
        dialog_first = view.findViewById(R.id.dialog_first);
        dialog_second = view.findViewById(R.id.dialog_second);

        custom_channel.setOnClickListener(this);
        search_channel.setOnClickListener(this);

        dialog_first.addChangingListener(this);
        dialog_second.addChangingListener(this);
        dialog_quxiao.setOnClickListener(this);
        dialog_queding.setOnClickListener(this);

        dialog_first.setViewAdapter(new ArrayWheelAdapter<String>(context, strs));
        dialog_first.setCyclic(true);
        dialog_first.setCurrentItem(shi-1);
        first = strs[shi-1];
        dialog_second.setViewAdapter(new ArrayWheelAdapter<String>(context, strs));
        dialog_second.setCyclic(true);
        dialog_second.setCurrentItem(ge-1);
        second = strs[ge-1];

        popupWindow = new PopupWindow(context);
        popupWindow.setContentView(view);
        popupWindow.setWidth(BaseStatic.SCREEN_WIDTH / 3);
        popupWindow.setHeight(BaseStatic.SCREEN_HEIGHT / 4);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setFocusable(true);

        popupWindow.showAsDropDown(view, BaseStatic.SCREEN_HEIGHT / 2 - BaseStatic.SCREEN_WIDTH / 6, BaseStatic.SCREEN_WIDTH / 2 - BaseStatic.SCREEN_HEIGHT / 8);
    }

    @Override
    public void onClick(View view) {
        if (view == dialog_quxiao) {
            popupWindow.dismiss();

        } else if (view == dialog_queding) {
            call.Select(first, second);
            popupWindow.dismiss();

        } else if (view == search_channel) {
            custom_channel.setVisibility(View.GONE);
            select_channel.setVisibility(View.GONE);
            select_ok_or_cancel.setVisibility(View.GONE);
            current_channel.setVisibility(View.VISIBLE);
            //同时触发搜索事件
            call.Search(current_channel);

        } else if (view == custom_channel) {
            search_channel.setVisibility(View.GONE);
            current_channel.setVisibility(View.GONE);
            select_channel.setVisibility(View.VISIBLE);
            select_ok_or_cancel.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == dialog_first) {
            first = strs[newValue];
        } else if (wheel == dialog_second) {
            second = strs[newValue];
        }
    }
}
